/*
 * BenchmarkDailyDumpCheckJob
 * Created: Dec 19, 2006 
 * by abargnesi<mailto:abargnesi@aquent.com/>
 */
package com.den4.jynform.tasks;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.den4.jynform.alerts.EmailLogger;
import com.den4.jynform.alerts.GoogleTalkLogger;

public class PingSiteJob implements Job {
    private EmailLogger emailLogger;
    private GoogleTalkLogger googleTalkLogger;
    private String taskMsg;
    private String taskSubject;
    private Log log = LogFactory.getLog(PingSiteJob.class);
    private String siteLocation;
    private int connectionTimeout = 30000;
    private int sitePort = 80;
    private boolean reportSuccess;
    
    public void execute(JobExecutionContext ctx) throws JobExecutionException {
        Map jobMap = ctx.getJobDetail().getJobDataMap();
        
        //Aquire loggers
        emailLogger = (EmailLogger) jobMap.get("emailLogger");
        googleTalkLogger = (GoogleTalkLogger) jobMap.get("googleTalkLogger");
        
        //Connect google logger.
        googleTalkLogger.connect();
        
        //Aquire connection properties
        siteLocation = (String) jobMap.get("siteLocation");
        if(siteLocation == null) {
            taskSubject = "Site Ping on - " + "NULL Site Location.";
            taskMsg = "Site location is null.  Not reachable because of misconfiguration.";
            log.info(taskSubject);
            log.info(taskMsg);
            googleTalkLogger.log(taskSubject, taskMsg);
        }
        
        parseSiteLocation(siteLocation);
        reportSuccess = (Boolean) jobMap.get("reportSuccess");
        
        taskSubject = "Site Ping on - " + siteLocation.concat(":").concat(String.valueOf(sitePort));
        try {
            InetAddress address = InetAddress.getAllByName(siteLocation)[0];
            Socket s = new Socket();
            s.connect(new InetSocketAddress(address, sitePort), 30000);
            
            //Report a success if necessary.
            if(reportSuccess) {
                taskMsg = "Ping successful, site reachable.";
                log.info(taskSubject);
                log.info(taskMsg);
                googleTalkLogger.log(taskSubject, taskMsg);
                //emailLogger.logEmail(taskSubject, taskMsg);
            }
        } catch (Exception e) {
            //Connection to site at location and port failed.
            taskMsg = "Ping failed, yielding error:\n\n".concat(e.getMessage());
            log.info(taskSubject);
            log.info(taskMsg);
            googleTalkLogger.log(taskSubject, taskMsg);
            //emailLogger.logEmail(taskSubject, taskMsg);
        } finally {
            //Disconnect google logger.
            googleTalkLogger.disconnect();
        }
    }
    
    private void parseSiteLocation(String siteLocation) {
        if(siteLocation.indexOf(':') != -1) {
            String[] siteTokens = siteLocation.split(":");
            this.siteLocation = siteTokens[0].trim();
            this.sitePort = Integer.parseInt(siteTokens[1].trim());
        } else {
            this.siteLocation = siteLocation;
        }
    }
}
